package com.crop.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.crop.back.common.enums.Deleted;
import com.crop.back.common.utils.ResultBean;
import com.crop.back.common.utils.UserHolder;
import com.crop.back.dto.UserDTO;
import com.crop.back.entity.Supplier;
import com.crop.back.service.SupplierService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.annotation.Resource;

/**
 * <p>
 * 供应商信息表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2025-01-20
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    @GetMapping("/get")
    public ResultBean<Supplier> getInfo() {
        UserDTO user = UserHolder.getUser();
        Supplier one = supplierService.getOne(new LambdaQueryWrapper<Supplier>()
                .eq(Supplier::getDeleted, Deleted.NOT_DELETED.getCode())
                .eq(Supplier::getId, user.getReferenceId()));
        return ResultBean.ok(one);
    }

    @PostMapping("/save")
    public ResultBean<String> update(@RequestBody Supplier supplier) {
        supplierService.updateById(supplier);
        return ResultBean.ok("保存成功");
    }

    @GetMapping("/name")
    public ResultBean<List<Supplier>> getAll() {
        UserDTO user = UserHolder.getUser();
        List<Supplier> list = supplierService.list(new LambdaQueryWrapper<Supplier>()
                .eq(Supplier::getDeleted, Deleted.NOT_DELETED.getCode())
                .eq(Supplier::getId, user.getReferenceId()));
        return ResultBean.ok(list);
    }

}
